import java.io.*;
import java.util.Scanner;
import java.sql.*;


public class Login {
	
	public static void main(String[] args)
	{
		read();
	}
	
	
	public static void read()
	{
		String id = null;
		String pw = null;
		String temp1 = null;
		String temp2 = null;
		
		boolean bool;
		
		// db연결
		Connection conn = null;
		Statement stmt = null;
		
		try {
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Bank", "root", "1234");
		
		if (conn == null)
			throw new Exception("데이터베이스 연결할 수 없음");

		
		Scanner scan = new Scanner(System.in);
		System.out.print("ID: ");
		id = scan.next();
		
		stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("select id, pw from customer where id= '"+id+"';");
		bool = rs.next();
		
		
		// 입력한 아이디가 틀렸을 경우
		while(bool == false){
			System.out.println("입력하신 id가 틀렸거나 없는 id입니다.");
			System.out.print("ID: ");
			id = scan.next();
			
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select id, pw from customer where id= '"+id+"';");	
			bool = rs.next();
		}
		
		// 입력한 id값이 디비에 있을 경우나 admin을 받았을 경우 
		while(bool == true || id.equals("admin"))
		{
			temp1 = rs.getString("id");
			temp2 = rs.getString("pw");
						
			if(id.equals("admin"))
			{	//admin page로 넘어감
				new admin();
			}			
			// id 비교
			else if(temp1.equals(id))		
			{
				System.out.println("id: "+id);
				
				// password 입력  
				Scanner scan3 = new Scanner(System.in);
				System.out.print("Password: ");
				pw = scan3.next();
				
				
				// password 비교
				if(temp2.equals(pw)) {
					// customer page로 넘어감
					new customer(id, pw);
				}
				else
					System.out.println("비밀번호가 틀렸습니다.");
			}
			
			Scanner scan2 = new Scanner(System.in);
			System.out.print("ID: ");
			id = scan2.next();
			
			rs = stmt.executeQuery("select id, pw from customer where id= '"+id+"';"); 
			bool = rs.next();
			
		}
		
		}catch(Exception e) {
			System.out.println(e);
			
		}
		finally {
			try {
				conn.close();
			} catch (Exception ignored) {
				
			}
		}
		
	}
}

